package com.hourses.controller;

import com.hourses.pojo.ProductType;
import com.hourses.pojo.Result;
import com.hourses.service.impl.ProductTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/8 14:54
 */

//商品分类控制器
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {
    //获取商品分类树   http://localhost:3000/api/productCategory/product-category-tree
    @Autowired
    private ProductTypeServiceImpl productTypeService;
    @GetMapping("/product-category-tree")
    public Result getProductTypeTree(){
        List<ProductType> productTypes = productTypeService.selectAllTypeTree();
        return Result.ok(productTypes);
    }


    //添加商品分类时检查type_code是否已经存在  http://localhost:3000/api/productCategory/verify-type-code?typeCode=123
    @GetMapping("/verify-type-code")
    public Result checkProductCodeIsExist(@RequestParam("typeCode")String typeCode){
        ProductType productType = new ProductType();
        productType.setTypeCode(typeCode);
        //查询product_type表中是否有该productCode
        Result result = productTypeService.selectByTypeCode(productType);
        return result;
    }

    //添加商品分类   http://localhost:3000/api/productCategory/type-add POST
    //向product_type表中插入数据
    @PostMapping("/type-add")
    public Result addProductType(@RequestBody ProductType productType){
        //插入数据之前要先检查数据库product_type表中有没有与即将插入的数据的type_name字段相同的，type_name字段不能重复
        ProductType productType1 = productTypeService.selectByTypeName(productType.getTypeName());
        if (productType1==null){
            //数据库product_type表中没有与即将插入的数据的type_name字段相同的productType
            //往productType表中进行增加数据的操作
            Result result = productTypeService.insertProductType(productType);
            return result;
        }
        //数据库product_type表中有与即将插入的数据的type_name字段相同的productType
        return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！分类名称已经存在...");
    }

    //删除商品分类   http://localhost:3000/api/productCategory/type-delete/21
    @RequestMapping("/type-delete/{typeId}")
    public Result deleteProducttype(@PathVariable Integer typeId){
        Result result = productTypeService.deleteProducttype(typeId);
        return result;
    }

    //修改商品分类   http://localhost:3000/api/productCategory/type-update
    @RequestMapping("/type-update")
    public Result updateProductType(@RequestBody ProductType productType){
        Result result = productTypeService.updateProductType(productType);
        return result;
    }
}
